package com.js.purs.service.impl;

import java.util.List;

import com.js.purs.domain.PurAvoidExpert;
import com.js.purs.mapper.PurAvoidExpertMapper;
import com.js.purs.mapper.PurConditionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.js.purs.mapper.PurBindExpertMapper;
import com.js.purs.domain.PurBindExpert;
import com.js.purs.service.IPurBindExpertService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 招标专家关联Service业务层处理
 *
 * @author lzq
 * @date 20240704
 */
@Service
public class PurBindExpertServiceImpl implements IPurBindExpertService {
    @Autowired
    private PurBindExpertMapper purBindExpertMapper;

    @Autowired
    private PurConditionMapper purConditionMapper;

    @Autowired
    private PurAvoidExpertMapper avoidExpertMapper;

    /**
     * 查询招标专家关联
     *
     * @param bindExpertId 招标专家关联主键
     * @return 招标专家关联
     */
    @Override
    public PurBindExpert selectPurBindExpertByBindExpertId(Long bindExpertId) {
        return purBindExpertMapper.selectPurBindExpertByBindExpertId(bindExpertId);
    }

    /**
     * 查询招标专家关联列表
     *
     * @param purBindExpert 招标专家关联
     * @return 招标专家关联
     */
    @Override
    public List<PurBindExpert> selectPurBindExpertList(PurBindExpert purBindExpert) {
        return purBindExpertMapper.selectPurBindExpertList(purBindExpert);
    }

    /**
     * 新增招标专家关联
     *
     * @param purBindExpert 招标专家关联
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPurBindExpert(List<PurBindExpert> purBindExpert) {
        int index = 0;
        System.out.println(purBindExpert.size());
        if (purBindExpert.size() > 0) {
            PurBindExpert bindExpert = purBindExpert.get(0);
            List<PurBindExpert> purBindExperts = purBindExpertMapper.selectPurBindExpertList(bindExpert);
            for (PurBindExpert expert : purBindExperts) {
                //删除
                index = purBindExpertMapper.deletePurBindExpertByBindExpertId(expert.getBindCode());
            }
            for (PurBindExpert expert : purBindExpert) {
                index = purBindExpertMapper.insertPurBindExpert(expert);
            }
        } else {
            for (PurBindExpert expert : purBindExpert) {
                index = purBindExpertMapper.insertPurBindExpert(expert);
            }
        }

        return index;
    }

    /**
     * 修改招标专家关联
     *
     * @param purBindExpert 招标专家关联
     * @return 结果
     */
    @Override
    public int updatePurBindExpert(PurBindExpert purBindExpert) {
        return purBindExpertMapper.updatePurBindExpert(purBindExpert);
    }

    /**
     * 批量删除招标专家关联
     *
     * @param bindExpertIds 需要删除的招标专家关联主键
     * @return 结果
     */
    @Override
    public int deletePurBindExpertByBindExpertIds(Long[] bindExpertIds) {
        return purBindExpertMapper.deletePurBindExpertByBindExpertIds(bindExpertIds);
    }

    /**
     * 删除招标专家关联信息
     *
     * @param bindCode 招标专家关联主键
     * @return 结果
     */
    @Override
    public int deletePurBindExpertByBindExpertId(String bindCode) {
        return purBindExpertMapper.deletePurBindExpertByBindExpertId(bindCode);
    }

    @Override
    public int deletePurBindExpertByExpertCode(String expertCode) {
        return purBindExpertMapper.deletePurBindExpertByExpertCode(expertCode);
    }

    @Override
    public int delExpertBatch(String[] codeBatch) {
        int index = 0;
        for (String batch : codeBatch) {
            index = purBindExpertMapper.deletePurBindExpertByExpertCode(batch);
        }
        return index;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int clearExpertByBindCode(String bindCode) {
        int index = 0;
        index = purBindExpertMapper.deletePurBindExpertByBindExpertId(bindCode);
        index = purConditionMapper.deletePurConditionByConditionId(bindCode);
        index = avoidExpertMapper.deletePurAvoidExpertByBindCode(bindCode);
        return index;
    }

    @Override
    public int insertBindExpertSingle(PurBindExpert purBindExpert) {

        return purBindExpertMapper.insertPurBindExpert(purBindExpert);
    }


}
